from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View
from .forms import MyForm, SignUpForm
from .models import User


# TODO: 首页
class IndexView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'index.html')

    def post(self, request, *args, **kwargs):
        form = MyForm(request.POST)
        if form.is_valid():
            # price = form.cleaned_data.get('price')
            # print(price)
            return HttpResponse('success')
        else:
            print(form.errors.get_json_data())
            return HttpResponse('fail')


# TODO: 注册
class SignUpView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'signup.html')

    def post(self, request, *args, **kwargs):
        form = SignUpForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            telphone = form.cleaned_data.get('telphone')
            psd1 = form.cleaned_data.get('psd1')
            User.objects.create(username=username, telphone=telphone, password=psd1)
            return HttpResponse('success')
        else:
            print(form.get_errors())
            return HttpResponse('fail')
